<template>
    <el-drawer :title="title || '添加名单'" :visible="visible" size="50%" @close="handleClose" :append-to-body="true"
        :modal-append-to-body="false">
        <div class="drawer">
            <div class="user-search">
                <el-input v-model="searchKey" placeholder="请输入手机号或者邮箱搜索" class="management__headerInput" :clearable="true"
                    style="width: 300px" />
                <el-button type="primary" size="small" style="margin-right: 88px" @click="fetchUsers">搜索</el-button>
            </div>

            <div class="user-associate">
                <el-button type="success" size="small" @click="handleSelect">添加</el-button>
            </div>

            <el-table @selection-change="handleSelectionChange" :data="users" style="width: 100%"
                height="calc(100vh - 280px)" align="center" header-align="center" v-loading="loading">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column label="手机/邮箱账号" prop="account" />
                <el-table-column label="姓名" prop="name" />
                <el-table-column label="身份证" prop="idNumber" />
                <el-table-column label="工作单位" prop="workUnit" />
                <el-table-column label="职位" prop="position" />
            </el-table>

            <div class="pagination-container">
                <el-pagination :current-page="pageNum" :page-size="pageSize" layout="prev, pager, next" :total="total"
                    background @current-change="handlePageChange" />
            </div>
        </div>
    </el-drawer>
</template>

<script>
export default {

    props: ['title', "visible"],
    data() {
        return {
            loading: false,
            visibleDrawer: false,
            pageNum: 1,
            pageSize: 20,
            total: 0,
            users: [],
            searchKey: "",
        }
    },

    mounted() {
        this.fetchUsers();

    },


    methods: {

        async fetchUsers() {
            this.loading = true;
            const params = {
                pageNum: this.pageNum,
                pageSize: this.pageSize,
                account: this.searchKey,
                type: "normal"
            };
            try {
                const { data } = await this.$fetch.get("/api/user/list", params);
                this.users = data.sorts;
                this.total = data.total;
            } catch (error) {
                console.log(error);
            }
            this.loading = false;
        },

        handlePageChange(pageNum) {
            this.pageNum = pageNum;
            this.fetchUsers();
        },

        handleSelectionChange(val) {
            this.multipleSelection = val;
        },


        async handleSelect() {
            this.$emit("select", this.multipleSelection)
        },

        handleClose() {
            this.$emit("update:visible", false)
        }
    }
}
</script>

<style scoped lang="less">
.drawer {
    padding: 0 20px;

    .user-search {
        display: flex;
        flex-direction: row;
        padding-bottom: 20px;
        border-bottom: 1px solid #ebeef5;
        ;

        .el-input {
            width: 200px;
        }
    }

    .user-associate {
        margin-top: 10px;
    }

    .pagination-container {
        display: flex;
        justify-content: center;
        padding: 20px 0;
    }

}
</style>
